/*
 * @name: 校园品牌说
 * @Author: along
 * @Date: 2020-06-01
 */
<template>
    <div class="container zdWebsitBrand">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">品牌说:</p>
                    <el-input
                        v-model="head.brand"
                        placeholder="请输入内容"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">公司名称:</p>
                    <el-input
                        v-model="head.name"
                        placeholder="请输入内容"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
            </div>
            <div class="header-right">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="()=>{
                            this.$router.push({
                                name: 'zdWebsitBrandChange',
                                query: {
                                    id: ''
                                }
                            })
                        }"
                    >
                        新建品牌说
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="zdWebsitBrand"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="brand_name"
                label="品牌说名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="brand_people"
                label="客户姓名"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="company_name"
                label="公司名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="category_title"
                label="所在行业"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.sub_category_title + '-' + scope.row.category_title }}
                </template>
            </el-table-column>
            <el-table-column
                prop="brand_title"
                label="标题"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="brand_info"
                label="内容"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="brand_cover"
                label="图片"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p
                        @click="fnShowPreview(scope.row.brand_cover)"
                        class="cursor-font"
                    >
                        查看图片
                    </p>
                    <!-- <img
                        :src="scope.row.brand_cover"
                        alt="加载失败"
                        @click="fnShowPreview(scope.row.brand_cover)"
                        style="width:142px;height:56px"
                    > -->
                </template>
            </el-table-column>
            <el-table-column
                prop="brand_sort"
                label="排序"
                align="center"
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.brand_sort }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            @click="fnEditSort(scope.row)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="brand_show"
                label="状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.brand_show == 1">已显示</p>
                    <p v-if="scope.row.brand_show == 0">已隐藏</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="shelf_time"
                label="开始时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.shelf_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="expire_time"
                label="结束时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.expire_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                fixed="right"
                :width="150"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnEditBrand(scope.row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnBrandDisplay(scope.row)"
                        v-if="scope.row.brand_show == 0"
                    >
                        显示
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnBrandDisplay(scope.row)"
                        v-if="scope.row.brand_show == 1"
                    >
                        隐藏
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnDeleteBrand(scope.row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import { back } from '@/mixin/back';
export default {
    /**
     * @param   {Object}     head                       筛选字段
     * @param   {Object}     page                       分页
     * @param   {Array}      tableData                  数据列表
     * @param   {OBject}     authResult                 权限列表
     */
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            head: {
                brand: '',
                name: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '官网'},{title: '官网自定义'},{title: '校园品牌说'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {
        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/wei4/hire_brand%5Cget_list', {
                page_id: page || this.page.now_page,
                brand_name: this.head.brand,
                company_name: this.head.name
            }, resp => {
                if (resp.code == 1) {
                    if(Object.prototype.toString.call(resp.data.list) == '[object Object]') {
                        this.tableData = [
                            resp.data.list
                        ];
                    } else if(Object.prototype.toString.call(resp.data.list) == '[object Array]') {
                        this.tableData = resp.data.list;
                    }
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 编辑
         */
        fnEditBrand (row) {
            this.$router.push({
                name: 'zdWebsitBrandChange',
                query: {
                    id: row.brand_no
                }
            });
        },

        /**
         * 显示or隐藏
         */
        fnBrandDisplay (row) {
            //判断最多显示6个
            this.$post('/wei4/hire_brand%5Cmodify',{
                brand_no: row.brand_no,
                brand_sort: row.brand_sort,
                brand_show: row.brand_show == 1 ? 0 : 1
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData();
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    });
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 删除品牌说
         */
        fnDeleteBrand (row) {
            this.$confirm('是否确认删除?', '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/wei4/hire_brand%5Cdelete',{
                    brand_no: row.brand_no
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '删除成功'
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg
                        });
                    }
                });
            });
        },

        /**
         * 排序
         */
        fnEditSort (row) {
            this.$prompt('请输入排序号', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^([1-9]\d?|[1-9]\d+)$/,
                inputErrorMessage: '排序号格式不正确,请重新输入'
            }).then(({ value }) => {
                this.$post('/wei4/hire_brand%5Cmodify',{
                    brand_no: row.brand_no,
                    brand_sort: value,
                    brand_show: row.brand_show
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '操作成功'
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg
                        });
                    }
                });
            });
        },

        /**
         * 查看图片
         */
        fnShowPreview(images) {
            this.$refs.preview.show([
                images
            ]);
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
</style>
<style lang="less">
    .zdWebsitBrand{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .el-button+.el-button {
            margin-left: 0px;
            text-align: center;
        }
        .el-button {
        }
    }
</style>